<?php
/**
 * 系统设置模型管理
 * @author cjli
 *
 */
class SiteModel extends Model
{
	/**
	 * 获取所有地区列表
	 */
	public static function get_region_list()
	{
		$regionList = array();
		if(S('region_list')){
			$regionList = S('region_list');
		}else{
			$region_db = D('region');
			$regionList = $region_db->field('id,name,iso2_code,iso3_code,country_number,pid')->where('status=1')->order('id ASC')->select();
			S('region_list', $regionList);
		}
		return $regionList;
	}
	
	/**
	 * 获取地区信息
	 * @param int $id
	 */
	public static function get_region_info($id)
	{
		$region_db = D('region');
		$regionInfo = $region_db->field('name,pid')->find($id);
		return $regionInfo;
	}
	
	/**
	 * 获取国家列表
	 */
	public static function get_country_list()
	{
		$countryList = array();
		if(S('country_list')){
			$countryList = S('country_list');
		}else{
			$region_db = D('region');
			$countryList = $region_db->field('id,name')->where('pid=0 AND status=1')->select();
			/*foreach ($result as $v){
				$countryList[$v['id']] = $v['name'];
			}*/
			S('country_list', $countryList);
		}
		return $countryList;
	}
	
	/**
	 * 获取省,市列表
	 * @param int $pid 上级ID号
	 * @return ArrayObject
	 */
	public static function get_area_list($pid = 0)
	{
		$areaArray = array();
		$pid = intval($pid);
		$region_db = D('region');
		$areaArray = $region_db->field('id,name')->where('pid='.$pid.' AND status=1')->select();
		return $areaArray;
	}
	/**
   * 前台获取货币列表
   * @return array
   */
	public static function currency_list()
	{
		$defaultCurrency = C('currency');
		$currencyList = $currencyArray = array();
		if(S('currency_list')){
			$currencyList = S('currency_list');
		}else{
			$currency_db = D('currency');
			$currencyList = $currency_db->where('visible=1')->select();
			S('currency_list', $currencyList);
		}
		foreach ($currencyList as $curr)
		{
			$curr['isdefault'] = ($defaultCurrency == $curr['name']);
			$currencyArray[$curr['name']] = $curr;
		}
		return $currencyArray;
	}
	
	/**
	 * 通过货币代码获取货币信息
	 * @param string $name 货币代码符号
	 * @return array
	 */
	public static function get_currency_info($name)
	{
		$currencyList = self::currency_list();
		return isset($currencyList[$name]) ? $currencyList[$name] : false; 
	}
	
	/**
	 * 获取友情链接列表
	 * @param int $link_type 友情链接类型
	 * @return array
	 */
	public static function get_link_list($link_type)
	{
		$link_type = intval($link_type) ? 1 : 0;
		if(S('link_list_'.$link_type)){
			$linkList = S('link_list_'.$link_type);
		}else{
			$link_db = D('link');
			$link_db->field('name,url,logo');
			$link_db->where(array('link_type' => $link_type, 'disabled' => 1));
			$link_db->order('istop DESC, listorder DESC, id ASC');
			$linkList = $link_db->select();
			S('link_list_'.$link_type, $linkList);
		}
		return $linkList;
	}
	
	/**
	 * 语言列表
	 */
	public static function get_language_list()
	{
		$languageList = array();
		if(S('model_language_list')){
			$languageList = S('model_language_list');
		}else{
			$lang_db = D('language');
			$languageArray = $lang_db->field('id,name,code,directory,locale')->where('status=1')->order('listorder DESC')->select();
			if($languageArray){
				foreach ($languageArray as $lang){
					$languageList[$lang['id']] = $lang;
				}
			}
			S('model_language_list', $languageList);
		}
		return $languageList;
	}
	
	/**
	 * 获取语言包信息
	 * @param $language_id 语言ID
	 */
	public static function get_language_info($language_id)
	{
		$languageList = self::get_language_list();
		return $languageList[$language_id] ? $languageList[$language_id] : 1;
	}
	
	/**
	 * 获取站点配置数据
	 */
	public static function get_shop_config_list()
	{
		$configList = array();
		if(S('shop_config_list')){
			$configList = S('shop_config_list');
		}else{
			$config_db = D('shop_config');
			$configArray = $config_db->field('config_name,config_value')->select();
			if($configArray){
				foreach ($configArray as $con){
					$configList[$con['config_name']] = $con['config_value'];
				}
			}
			S('shop_config_list', $configList);
		}
		return $configList;
	}
	
}